﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB05AV95NData : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public AB05AV95NData()
            : base()
        {

        }
        public AB05AV95NData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        
        public AB05AV95NData(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        
        ~AB05AV95NData()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();

                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ARTICLE"].ToString();
                string STARTDATE = dwABPOS["STARTDATE"].ToString();
                string ENDDATE = dwABPOS["ENDDATE"].ToString();
                or1.CommandText = "SELECT A.ICCABSERLKZ AS STATUS,A.ICCABSFACTORY,A.ICCABSAUFKRS||'-'||TO_NUMBER(A.ICCABSAUFLFD) "
                    + " ||'-'||TO_NUMBER(A.ICCABSAUFPOS)||'-'||TO_NUMBER(A.ICCABSITP) AS FORDERNO,A.ICCABSIDAT, "
                    + " TRIM(A.ICCABSART)||'-'||TRIM(A.ICCABSFARBE)||'-'||TRIM(A.ICCABSVAR) AS ARTICLE, "
                    + " NVL(E.COLORDES,A.ICCABSBESCHICH) AS FINISHING,A.ICCABSDPART,A.ICCABSIPCNUM, "
                    + " DECODE(A.ICCABSIWAKZ,'F',ROUND(A.ICCABSIQTY/0.9144*0.001,2),A.ICCABSIQTY*0.001) AS ICCABSIQTY, "
                    + " A.ICCABSRDAT,A.ICCABSRPCNUM,ROUND(A.ICCABSRQTY/0.9144*0.001,2) AS ICCABSRQTY, "
                    + " (case when A.ICCABSIQTY = 0 then 0 else ROUND((A.ICCABSIQTY-A.ICCABSRQTY)/A.ICCABSIQTY,3) end) AS RATE "
                    + " FROM ICC_ABPOS_STATUS A,(SELECT DISTINCT SUBSTR(SDKEY,3,10) as COLOR, "
                    + " SUBSTR(SDDATEN,2,30) AS COLORDES FROM SDDAT "
                    + " WHERE SUBSTR(SDKEY,1,2)='00' AND SDSA = '9998') E "
                    + " WHERE A.ICCABSBESCHICH = E.COLOR(+) "
                    + " AND A.ICCABSTYPE = 'F' "
                    + " AND ((A.ICCABSERLKZ = :ABPERLKZ  "
                    + "       AND ((:TP = ' ' AND A.ICCABSIPCNUM - A.ICCABSRPCNUM >0) OR :TP ='J') ) "
                    + "     OR :ABPERLKZ = 'J' ) "
                    + " AND A.ICCABSIDAT >= '" + STARTDATE + "' "
                    + " AND A.ICCABSIDAT <= '" + ENDDATE + "' "
                    + " AND A.ICCABSAUFKRS LIKE '%" + ABPAUFKRS
                    + "%' AND A.ICCABSAUFLFD LIKE '%" + ABPAUFLFD
                    + "%' AND A.ICCABSAUFPOS LIKE '%" + ABPAUFPOS + "%' "
                    + " AND TRIM(A.ICCABSART)||'-'||TRIM(A.ICCABSFARBE)||'-'||TRIM(A.ICCABSVAR) LIKE '%" + ABPART + "%' "
                   ;
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                or1.Parameters.Add("TP", OracleType.NVarChar).Value = dwABPOS["TP"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";                  
                    byte[] b;
                    string value;
                    string Column = "";
                    string status = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        status = dwTemp["STATUS"].ToString().Trim();
                        if(status == "J")
                        {
                            dwTemp["STATUS"] = "完成";
                        }
                        Column = "ICCABSFACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "FINISHING";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }    
        #endregion
    }
}